﻿using System;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace SchemaMonitor.json
{
    [DataContract]
    public class CFieldComposite : CField
    {
        [DataMember(Order = 1)]
        public List<CNodeBasic> Composites { get; set; }

        //Constructor
        public CFieldComposite(CNodeBasic parent, CTag tag, Dictionary<string, object> value, CUser c) : base(parent, tag)
        {
            this.Value = new CNodeBasic(value, this, tag, c);
        }
        public CFieldComposite(CNodeBasic parent, CTag tag, CNodeBasic value) : base(parent, tag)
        {
            this.Value = value;
        }
        public CFieldComposite(CNodeBasic parent, CTag tag, object[] array, CUser c) : base(parent, tag)
        {
            var list = new List<CNodeBasic>(array.Length);
            foreach (Dictionary<string, object> i in array)
                list.Add(new CNodeBasic(i, this, tag, c));

            this.Composites = list;
        }

        //Serialisation Constructors
        private CFieldComposite() { }
        static CFieldComposite() { ProtoBuf.Serializer.PrepareSerializer<CFieldComposite>(); }

        //Must Override
        public List<CNodeBasic> List_
        {
            get { return Composites; }
            set { Composites = value; }
        }
        public override IList List
        {
            get { return Composites; }
            set
            {
                if (value is List<CNodeBasic>)
                    this.List_ = (List<CNodeBasic>) value;
                else
                    this.Values = value.Cast<object>().ToArray();
            }
        }
        protected override IList InitList()                 { return new List<CNodeBasic>(); }
        protected override IList InitList(object[] array)   { return array.Cast<CNodeBasic>().ToList(); }

    }
}
